package com.wjk.kylin.mall.common.web.filter;

import com.wjk.kylin.mall.common.core.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by cimi on 2017/5/12.
 */
@Slf4j
@WebFilter(urlPatterns = {"/*"}, filterName = "requestWrapperFilter")
public class RequestWrapperFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PathMatcher pathMatcher = new AntPathMatcher();
        String path = ((HttpServletRequest) request).getRequestURI();

        if (pathMatcher.match(Constants.ACTUATOR_URL_PATTERN, path)) {
            //filter链往下走
            chain.doFilter(request, response);
        } else {
            WrappedHttpServletRequest requestWrapper = new WrappedHttpServletRequest((HttpServletRequest) request);

            // 这里doFilter传入我们实现的子类
            chain.doFilter(requestWrapper, response);
        }
    }

    @Override
    public void destroy() {
    }
}